package com.example.otherincome;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class TotalIncomeActivity extends AppCompatActivity {

    private Button btnClear;
    private TableLayout tableLayout;
    private TextView tvTotalIncome;

    private DatabaseHelper dbHelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_income);

        btnClear = findViewById(R.id.btnClear);
        tableLayout = findViewById(R.id.tableLayout);
        tvTotalIncome = findViewById(R.id.tvTotalIncome);

        dbHelper = new DatabaseHelper(this);
        db = dbHelper.getReadableDatabase();

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearIncomeRecords();
            }
        });

        displayIncomeRecords();
        updateTotalIncome();

        db.close();
    }

    private void displayIncomeRecords() {
        String query = "SELECT * FROM " + DatabaseContract.IncomeEntry.TABLE_NAME;

        Cursor cursor = db.rawQuery(query, null);

        if (tableLayout.getChildCount() > 1) {
            tableLayout.removeViews(1, tableLayout.getChildCount() - 1);
        }

        if (cursor.moveToFirst()) {
            do {
                @SuppressLint("Range") String income = cursor.getString(cursor.getColumnIndex(DatabaseContract.IncomeEntry.COLUMN_INCOME));
                @SuppressLint("Range") String category = cursor.getString(cursor.getColumnIndex(DatabaseContract.IncomeEntry.COLUMN_CATEGORY));
                @SuppressLint("Range") String timestamp = cursor.getString(cursor.getColumnIndex(DatabaseContract.IncomeEntry.COLUMN_TIMESTAMP));

                TableRow row = new TableRow(this);

                TextView tvIncome = new TextView(this);
                tvIncome.setText(income);
                row.addView(tvIncome);

                TextView tvCategory = new TextView(this);
                tvCategory.setText(category);
                row.addView(tvCategory);

                TextView tvTimestamp = new TextView(this);
                tvTimestamp.setText(timestamp);
                row.addView(tvTimestamp);

                tableLayout.addView(row);
            } while (cursor.moveToNext());
        }

        cursor.close();
    }

    private void clearIncomeRecords() {
        int rowsDeleted = db.delete(DatabaseContract.IncomeEntry.TABLE_NAME, null, null);

        if (rowsDeleted > 0) {
            Toast.makeText(this, "收入记录已清除", Toast.LENGTH_SHORT).show();
            tableLayout.removeViews(1, tableLayout.getChildCount() - 1);
            tvTotalIncome.setText("总收入: 0");
        } else {
            Toast.makeText(this, "清除失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void updateTotalIncome() {
        String query = "SELECT SUM(" + DatabaseContract.IncomeEntry.COLUMN_INCOME + ") FROM " +
                DatabaseContract.IncomeEntry.TABLE_NAME;

        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            double totalIncome = cursor.getDouble(0);
            tvTotalIncome.setText("总收入: " + totalIncome);
        }
        cursor.close();
    }
}
